//
//  CXNewFeatureController.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/21.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit

class CXNewFeatureController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
    }

    private func setupUI() {
        
        view.addSubview(scrollView)
        view.addSubview(pageControl)
        
        // 添加约束
        scrollView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view.snp_bottom).offset(-80)
        }
        
        //先给滚动视图添加约束，再给滚动视图添加图片
        for i in 0..<4 {
            let imgView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            imgView.frame = CGRect(x: CGFloat(i) * SCREENW, y: 0, width: SCREENW, height: SCREENH)
            scrollView.addSubview(imgView)
            
            if i == 3 {
                imgView.addSubview(sharedbutton)
                imgView.addSubview(enterButton)
                imgView.userInteractionEnabled = true
                //给这两个子控件进行布局
                sharedbutton.snp_makeConstraints { (make) -> Void in
                    make.centerX.equalTo(imgView)
                    make.bottom.equalTo(imgView.snp_bottom).offset(-150)
                }
                
                enterButton.snp_makeConstraints { (make) -> Void in
                    make.centerX.equalTo(sharedbutton)
                    make.top.equalTo(sharedbutton.snp_bottom).offset(5)
                }
            }
        }
        //设置滚动视图的滚动区域
        scrollView.contentSize = CGSize(width: CGFloat(pageControl.numberOfPages) * SCREENW, height: 0)
    }
    
    /// 滚动视图
    private lazy var scrollView:UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.pagingEnabled = true
        scrollView.bounces = false
        scrollView.delegate = self
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()
    /// 页数控制器
    private lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        // 禁止被点击
        pageControl.userInteractionEnabled = false
        pageControl.currentPage = 0
        pageControl.numberOfPages = 4
        // 设置默认颜色与选中的点的颜色
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.purpleColor()
        return pageControl
    }()
    //最后一个图片上的按钮和选框
    private lazy var sharedbutton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "new_feature_share_false"), forState: .Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: .Selected)
        button.setTitle("分享到微博", forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: -5)
        button.addTarget(self, action: "shareClick", forControlEvents: .TouchUpInside)
        return button
    }()
    
    private lazy var enterButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        button.setTitle("进入微博", forState: .Normal)
        //给按钮绑定事件
        button.addTarget(self, action: "enterClick", forControlEvents: .TouchUpInside)
        return button
    }()
}
// MARK: - 滚动代理方法
extension CXNewFeatureController : UIScrollViewDelegate {
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.frame.width + 0.5)
        print("当前页: \(page)")
        pageControl.currentPage = page
    }
}
// MARK: - 按钮点击事件；复选框和进入按钮
extension CXNewFeatureController {
    
    @objc private func shareClick() {
        sharedbutton.selected = !sharedbutton.selected
    }
    
    @objc private func enterClick() {
        print("新特性界面播放完毕,页面跳转")
        NSNotificationCenter.defaultCenter().postNotificationName(CXSwitchRootVCNotification, object: nil)
    }
}